package com.springboot.controller;

import com.google.common.collect.Lists;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.LoadBalancerBuilder;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.reactive.LoadBalancerCommand;
import com.netflix.loadbalancer.reactive.ServerOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import rx.Observable;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/house")
public class HelloController {
    @GetMapping("/hello")
    public String hello(){
        return "Hello World";
    }

    @ResponseBody
    @RequestMapping("/string")
    public String string(){
        return "string";
    }

    @ResponseBody
    @RequestMapping("/list")
    public List list(){
        List<String> list=new ArrayList<String>();
        list.add("a"); list.add("b"); list.add("c");
        return list;
    }

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/hello1")
    public String hello1(){
        return "Hello"+serverPort;
    }

   /* @GetMapping("/Ribbon")  //Ribbon调用接口
    public void Ribbon(){
        //服务列表
        List<Server> serverList= Lists.newArrayList(new Server("localhost",8082),new Server("localhost",8083));

        //构建负载实例
        ILoadBalancer loadBalancer= LoadBalancerBuilder.newBuilder().buildFixedServerListLoadBalancer(serverList);

        //调用5次来测试效果
        for(int i=0;i<5;i++){
            String result= LoadBalancerCommand.<String>builder().withLoadBalancer(loadBalancer).build().submit(new ServerOperation<String>() {
                @Override
                public Observable<String> call(Server server) {
                    try{
                        String addr="http://"+server.getHost()+":"+server.getPort()+"/house/hello";
                        System.out.println("调用地址："+addr);
                        URL url=new URL(addr);
                        HttpURLConnection conn=(HttpURLConnection)url.openConnection();
                        conn.setRequestMethod("GET");
                        conn.connect();
                        InputStream in=conn.getInputStream();
                        Data data = null;
                        return  Observable.just(new String(String.valueOf(data)));
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            }).toBlocking().first();
            System.out.println("调用结果："+result);
        }*/
    }

